package com.anyonavinfo.cpaddemo.view.musicplayerview;



import com.anyonavinfo.cpaddemo.data.musicinfo.MusicInfo;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.MusicInfoDataSource;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.MusicInfoRepository;
import com.anyonavinfo.cpaddemo.service.musicplayerservice.MusicPlayerService;
import com.anyonavinfo.cpaddemo.service.musicplayerservice.MusicPlayerServiceHelper;

import java.util.List;


/**
 * Created by ZuiC on 2017/2/10.
 */

public class MusicPlayerPresenter implements MusicPlayerContract.Presenter, MusicPlayerServiceHelper.MusicPlayerServiceCallBack{

    private MusicPlayerContract.View mView;
    private MusicInfoRepository mRepository;
    private MusicPlayerService mService;
    private MusicPlayerServiceHelper mServiceHelper;
    private int playMode, playStatus;
    private boolean isShow;


    public MusicPlayerPresenter(MusicPlayerContract.View view, MusicInfoRepository musicInfoRepository){
        mView = view;
        mView.setPresenter(this);
        this.mRepository = musicInfoRepository;
        mServiceHelper = MusicPlayerServiceHelper.getInstance();
        MusicPlayerServiceHelper.registerObserver(this);
        isShow = false;
    }

    @Override
    public void playStopCurMusic() {
        if (playStatus == 0){
            mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
                @Override
                public void onGetCurMusicInfo(MusicInfo musicInfo) {
                    mService.playMusic(musicInfo);
                }
            });

            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }else {
            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                    mService.pauseMusic();
                }
            });
        }
    }

    @Override
    public void playNextMusic() {
        if (playStatus == 0){
            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }
        mRepository.getNextMusicInfo(new MusicInfoDataSource.GetNextMusicInfoCallBack() {
            @Override
            public void onGetNextGetMusicInfo(MusicInfo musicInfo) {
                mService.playMusic(musicInfo);
                mView.updateMusicInfo(musicInfo);
            }
        });
    }

    @Override
    public void playPreviousMusic() {
        if (playStatus == 0){
            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }
        mRepository.getPreviousMusicInfo(new MusicInfoDataSource.GetPreviousMusicInfoCallBack() {
            @Override
            public void onGetPreviousMusicInfo(MusicInfo musicInfo) {
                mService.playMusic(musicInfo);
                mView.updateMusicInfo(musicInfo);
            }
        });
    }

    @Override
    public void changePlayMode() {
        mRepository.changeCurPlayMode(new MusicInfoDataSource.ChangeCurPlayModeCallBack() {
            @Override
            public void onChangeCurPlayMode(int mode) {
                playMode = mode;
                mView.updatePlayMode(playMode);
            }
        });
    }

    @Override
    public void refreshView() {
        isShow = true;
        mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
            @Override
            public void onGetCurMusicInfo(MusicInfo musicInfo) {
                mView.updateMusicInfo(musicInfo);
            }
        });

        mRepository.getCurPlayMode(new MusicInfoDataSource.GetCurPlayModeCallBack() {
            @Override
            public void onGetCurPlayMode(int mode) {
                mView.updatePlayMode(mode);
            }
        });

        mRepository.getCurPlayStatus(new MusicInfoDataSource.GetCurPlayStatusCallBack() {
            @Override
            public void onGetCurPlayStatus(int status) {
                mView.updatePlayStatus(status);
            }
        });
    }

    @Override
    public void onBackPressed() {
        isShow = false;
        mView.backToMainView();
    }

    @Override
    public void playSelectedMusic(int selectedPosition) {
        mRepository.setCurMusic(new MusicInfoDataSource.SetCurMusicCallBack() {
            @Override
            public void onSetCurMusic(MusicInfo musicInfo) {
                mView.updateMusicInfo(musicInfo);
                if (playStatus == 0){
                    mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                        @Override
                        public void onChangeCurPlayStatus(int status) {
                            playStatus = status;
                            mView.updatePlayStatus(status);
                        }
                    });
                }
                mService.playMusic(musicInfo);
            }
        },selectedPosition);
    }

    @Override
    public void deleteMusic(int position) {
        mRepository.deleteMusic(new MusicInfoDataSource.DeleteMusicCallBack() {
            @Override
            public void onDeleteMusic(List<MusicInfo> musicInfoList, boolean isSame, boolean isClear) {
                mView.updateMusicList(musicInfoList);
                if (isClear){
                    mService.stopMusic();
                }else {
                    if (isSame){
                        mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
                            @Override
                            public void onGetCurMusicInfo(MusicInfo musicInfo) {
                                mView.updateMusicInfo(musicInfo);
                                if (playStatus == 1){
                                    mService.playMusic(musicInfo);
                                }
                            }
                        });
                    }
                }
            }
        },position);
    }

    @Override
    public void changePlayProgress(final int percent) {
        if (playStatus == 0){
            mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
                @Override
                public void onGetCurMusicInfo(MusicInfo musicInfo) {
                    mService.playMusic(musicInfo);
                }
            });

            mRepository.changeCurPlayStatus(new MusicInfoDataSource.ChangeCurPlayStatusCallBack() {
                @Override
                public void onChangeCurPlayStatus(int status) {
                    playStatus = status;
                    mView.updatePlayStatus(status);
                }
            });
        }
        mRepository.getCurMusicInfo(new MusicInfoDataSource.GetCurMusicInfoCallBack() {
            @Override
            public void onGetCurMusicInfo(MusicInfo musicInfo) {
                mService.changePlayProgress(musicInfo.getDuration() * percent / 100);
            }
        });
    }

    @Override
    public void reloadMusicInfo() {
        mRepository.reloadMusicInfo(new MusicInfoDataSource.ReloadMusicInfoCallBack() {
            @Override
            public void onReloadMusicInfo(List<MusicInfo> musicInfoList) {
                mView.updateMusicList(musicInfoList);
            }
        });
    }

    @Override
    public void start() {
        mRepository.loadMusicInfo(new MusicInfoDataSource.LoadMusicInfoCallBack() {
            @Override
            public void onMusicInfoLoaded(List<MusicInfo> musicInfoList) {
                mView.initMusicList(musicInfoList);
            }
        });
    }


    @Override
    public void onMusicPlayBackEnding() {
        if (isShow){
            mRepository.getMusicInfoWhenEnding(new MusicInfoDataSource.getMusicInfoWhenEndingCallBack() {
                @Override
                public void onGetMusicInfoWhenEnding(MusicInfo musicInfo) {
                    mService.playMusic(musicInfo);
                    mView.updateMusicInfo(musicInfo);
                }
            });
        }
    }

    @Override
    public void onServiceConnected(MusicPlayerService musicPlayerService) {
        mService = musicPlayerService;
    }

    @Override
    public void updateProgress(int curProgress, int percent) {
        mView.updateProgress(curProgress, percent);
    }
}
